const BASE_URL = "http://localhost:8000";

// 添加请求拦截器
const httpInterceptor = {
    // 拦截前触发
    invoke(options) {
        // 非http开头需拼接地址
        if (!options.url.startsWith("http")) {
            options.url = BASE_URL + options.url;
        }

        // 请求超时
        options.timeout = 10000;

        // 设置请求头
        options.header = {
            ...options.header,
            "content-type": "application/json",
        };

        // 添加 token 请求头标识
        const token = uni.getStorageSync("token");
        if (token) {
            options.header.token = token;
        }
    },
};

uni.addInterceptor("request", httpInterceptor);
uni.addInterceptor("uploadFile", httpInterceptor);

export const http = (options) => {
    uni.showLoading({
        mask: true,
        title: "数据加载中...",
    });

    return new Promise((resolve, reject) => {
        uni.request({
            ...options,
            success(res) {
                const pages = getCurrentPages();
                const currentPage = pages[pages.length - 1];
                const currentRoute = currentPage.route;
                const routes = ["pages/login/login"];

                if (res.data.code === 0) { // 检查 res.data.code 是否为 0
                    if (res.data.result && res.data.result.token) {
                        console.log("存储的token:", res.data.result.token);
                        // 存储 token
                        uni.setStorageSync("token", res.data.result.token);

                        // 存储用户信息（可选）
                        const userInfo = res.data.result.user;
                        uni.setStorageSync("userInfo", userInfo);

                        resolve(res.data);
                    } else {
                        // uni.showToast({
                        //     icon: "none",
                        //     title: "登录成功，但未获取用户信息",
                        // });
                        resolve(res.data);
                    }
                } else if (res.data.code === 401 && !routes.includes(currentRoute)) {
                    uni.showModal({
                        title: "温馨提示",
                        content: "您还没有登录，是否去登录",
                        success(res) {
                            if (res.confirm) {
                                uni.navigateTo({
                                    url: "/pages/login/login",
                                });
                            } else {
                                uni.navigateBack({
                                    delta: 1,
                                });
                            }
                        },
                    });
                } else {
                    resolve(res.data);
                }
            },
            // 响应失败
            fail(err) {
                uni.showToast({
                    icon: "none",
                    title: "网络错误, 换个网络试试",
                });
                reject(err);
            },
            complete() {
                uni.hideLoading();
            },
        });
    });
};